<template>
  <div class="app-container">
    <el-form
      ref="form"
      :inline="true"
      :model="formData"
      class="demo-form-inline"
    >
      <el-form-item label="客户电话">
        <el-input
          v-model="formData.phoneNumber"
          placeholder="请输入客户电话"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button class="create" type="primary" @click="addBlack">新增</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="phoneNumber" label="客户电话"> </el-table-column>
      <el-table-column prop="reason" label="拉黑原因"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deleteItem(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加黑名单" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="dialogForm"
        :rules="dialogFormRules"
        ref="dialogForm"
        label-width="80px"
      >
        <el-form-item label="客户电话" prop="phoneNumber">
          <el-input
            v-model="dialogForm.phoneNumber"
            placeholder="请输入客户电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="拉黑原因" prop="reason">
          <el-input
            v-model="dialogForm.reason"
            placeholder="请输入拉黑原因"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import {
  getBlackList,
  addOrEditBlackList,
  removeBlackList,
} from "@/api/customer/index";
export default {
  name: "Index",
  data() {
    return {
      queryParams: {
        page: 0,
        pageSize: 10,
        total: 0,
      },
      formData: {
        phoneNumber: "",
      },
      list: [],
      dialogVisible: false,
      dialogForm: {
        phoneNumber: "",
        reason: "",
      },
      dialogFormRules: {
        phoneNumber: [
          { required: true, message: "请输入客户电话", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        reason: [
          { required: true, message: "请输入拉黑原因", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getListData({
      page: 1,
      limit: 10,
    });
  },
  methods: {
    addBlack() {
      this.dialogForm = {
        phoneNumber: "",
        reason: "",
      };
      this.dialogVisible = true;
    },
    submitForm() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          addOrEditBlackList(this.dialogForm).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.dialogVisible = false;
            this.getListData({
              page: 1,
              limit: 10,
            });
          });
        }
      });
    },
    getListData({ page, limit }) {
      getBlackList(
        this.queryParams,
        page || this.queryParams.page + 1,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },
    deleteItem(id) {
      this.$confirm("是否确认删除该黑名单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeBlackList(id).then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getListData({
            page: 1,
            limit: 10,
          });
        });
      });
    },
    onSearch() {
      this.queryParams = {
        ...this.queryParams,
        ...this.formData,
      };
      this.getListData({
        page: 1,
        limit: 10,
      });
    },
    reset() {
      this.formData = {
        phoneNumber: "",
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
